/******************************************************************************\
 * Filename   : enc_fifo.h
 * Copyright  : 
 * Created    : 2018-11-09 by litao
 * Description:
 * 
\******************************************************************************/

#ifndef __ENC_FIFO_H__
#define __ENC_FIFO_H__

#include <sys/types.h>
#include "pthread.h"
#include "uc.h"
#include <uc_utils.h>

#define QUEUE_MAX_UNIT_NUM	100

enum {
	QUEUE_NOBLOCK,
	QUEUE_BLOCK
};

typedef struct _carrier_buffer_t
{
	ucnode_s node;
	size_t private_flag;
    size_t data_len;    /* size of actual data */
	size_t data_max;    /* size of buffer data */
	size_t data_offset;
    uint8_t  data[0];   /* actuall buffer data */
}carrier_buffer_t;

typedef struct _qlist_body
{
	int node_count;
	int wait_sig;
	ucsig_s signal;
	uclock_s lock;
	uclist_s list;
}qlist_body;

typedef struct _cycle_queue
{
	qlist_body empty_fifo;
	qlist_body used_fifo;
}cycle_queue;	

/* Buffer management:
 * buffer_new()  - create new buffer of size i_size + plus header structure
 * buffer_free() - free buffer
 */
extern carrier_buffer_t *alloc_carrier_buffer(size_t i_size);
extern void free_carrier_buffer(carrier_buffer_t **buffer);
extern carrier_buffer_t *copy_carrier_buffer(carrier_buffer_t *p_src_carrier);

extern int initial_qlist_body(qlist_body *p_fifo);
extern int uninitial_qlist_body(qlist_body *p_fifo);
extern int initial_cycle_queue(cycle_queue *p_fifo);
extern int uninitial_cycle_queue(cycle_queue *p_fifo);

extern carrier_buffer_t *qlist_pop(qlist_body *fifo, int block_flag);
extern int qlist_push(qlist_body *fifo, carrier_buffer_t *buffer);

extern void qlist_destroy(qlist_body *fifo);
extern int qlist_count(qlist_body *fifo);

extern int qlist_safe_push(qlist_body *fifo, carrier_buffer_t *buffer);


#endif

